import React from 'react';
import {BrowserRouter as Router, Redirect, Route, Switch,NavLink} from 'react-router-dom';
import Home from './Home';
import routes from './router.config';

function PrivateRoute({routes,component:Com,...rest}){
	return(
		<Route
		{...rest}
		render={({ location }) =>
		(localStorage.getItem('fanmeiru')||localStorage.getItem('zhaoyunhui')||localStorage.getItem('xueziyue')||localStorage.getItem('lixing')||localStorage.getItem('yangqiping'))
		? 
		// <RouteWithSubRoutes {...rest}/>
		// <Com routes={{...rest}.routes}/>
		<Com routes={routes}/>
		// 登录
		: (<Redirect to={{
			pathname: "/login",
			state:location
		}} 
		/>
        )
	} 
    />
	)
}
function RouteWithSubRoutes(route) {
	if(!route.pri){
		return (
			<Route
				path={route.path}
				render={props => (
					<route.component {...props} routes={route.routes}/>
				)}
			/>
	    ); 
	}else{
		return <PrivateRoute {...route}/>;
	}
}



const App = () => {
    return <Router className="body">
		{/* <div className="content"> */}
        <Switch className="content">
			{
				routes.map(({pri,...rest})=>{
					if({...rest}.routes){
						if(!pri){
							return <RouteWithSubRoutes {...rest}/>
						}else{
							return <PrivateRoute {...rest}/>
						}
					}else{
						if(pri){
							console.log({...rest});
							return <PrivateRoute {...rest}/>
						}else{
							return <Route {...rest}/>
						}
					}
				})
			}
		</Switch>
		{/* </div> */}
        </Router>
}

export default App